import json


def merge_split_files():
    """
    合并cities_level2.json和districts_level3.json文件
    """
    try:
        # 读取城市级别文件
        with open("cities_level2.json", "r", encoding="utf-8") as f:
            cities_data = json.load(f)

        # 读取区县级别文件
        with open("districts_level3.json", "r", encoding="utf-8") as f:
            districts_data = json.load(f)

        # 合并数据
        merged_data = cities_data + districts_data

        # 保存合并结果
        with open("all_cities_districts.json", "w", encoding="utf-8") as f:
            json.dump(merged_data, f, ensure_ascii=False, indent=2)

        print(f"合并完成:")
        print(f"  城市级别: {len(cities_data)} 条")
        print(f"  区县级别: {len(districts_data)} 条")
        print(f"  合并总数: {len(merged_data)} 条")
        print(f"  保存至: all_cities_districts.json")

    except FileNotFoundError as e:
        print(f"文件未找到: {e}")
        print("请确保cities_level2.json和districts_level3.json文件存在")
    except Exception as e:
        print(f"合并错误: {e}")


if __name__ == "__main__":
    merge_split_files()
